package com.fhz.a2102shopjava.ui.dashboard.ui;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.fhz.a2102shopjava.R;
import com.fhz.a2102shopjava.base.BaseNoViewModelFragment;
import com.fhz.a2102shopjava.callback.ApiResponse;
import com.fhz.a2102shopjava.databinding.FragmentLeftBinding;
import com.fhz.a2102shopjava.databinding.FragmentRightBinding;
import com.fhz.a2102shopjava.entity.Category;
import com.fhz.a2102shopjava.pay.PayActivity;
import com.fhz.a2102shopjava.ui.dashboard.DashboardViewModel;
import com.fhz.a2102shopjava.ui.dashboard.ui.left.DashboardLeftRecyclerViewAdapter;
import com.fhz.a2102shopjava.ui.dashboard.ui.right.DashboardRightRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**

 */
public class RightFragment extends BaseNoViewModelFragment<DashboardViewModel, FragmentRightBinding> {

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_right;
    }

    private DashboardRightRecyclerViewAdapter mDashboardRightRecyclerViewAdapter;
    private List<Category> mCategoryList ;
    @Override
    protected void initData() {
        mCategoryList = new ArrayList<>();
        mBinding.rightFragmentRecyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
        mDashboardRightRecyclerViewAdapter = new DashboardRightRecyclerViewAdapter(getContext(),mCategoryList);
        mBinding.rightFragmentRecyclerView.setAdapter(mDashboardRightRecyclerViewAdapter);

        mViewModel.getCategory(1).observe(this, new Observer<ApiResponse<List<Category>>>() {
            @Override
            public void onChanged(ApiResponse<List<Category>> listApiResponse) {
                System.out.println(listApiResponse.getData().size());
                mCategoryList.addAll(listApiResponse.getData());
                mDashboardRightRecyclerViewAdapter.notifyDataSetChanged();
            }
        });

        mDashboardRightRecyclerViewAdapter.setItemClickListener((view, postion) -> PayActivity.launch(getActivity()));
    }

    @Override
    protected DashboardViewModel createViewModel() {
        return mViewModel = new ViewModelProvider(getParentFragment()).get(DashboardViewModel.class);
    }

}